package com.ht.psplan;

import com.ht.aems.communicate.PortListener;
import org.apache.log4j.Logger;

import java.io.UnsupportedEncodingException;
import java.util.Vector;

/**
 * port listener
 *
 * @author Dong Shufeng
 *         Date: 2007-2-9
 *         Time: 14:55:58
 */
public class MyPortListener implements PortListener {
    private static Logger log = Logger.getLogger(MyPortListener.class);
    int plantId = -1;
    boolean isSuccess = false;
    private String lastSuccessTime = null;

    public MyPortListener() {
    }

    public MyPortListener(int plantId) {
        this.plantId = plantId;
    }

    public boolean isSuccess() {
        return isSuccess;
    }

    public void dataUploaded(byte[] data) {
        try {
            String result = new String(data, "UTF8");
            log.info(result);
            String[] temp = result.split("#", 3);
            if (temp[2].trim().equals("1")) {
                lastSuccessTime = temp[1].trim();
                isSuccess = true;
            }
        } catch (Exception e1) {
            log.warn(e1);
        }
    }

    public void dataSent(byte[] data) {
        try {
            log.info(new String(data, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            log.warn(e);
        }
    }


    public boolean equals(Object obj) {
        if (obj instanceof MyPortListener)
            if (((MyPortListener) obj).plantId == this.plantId)
                return true;
        return false;
    }

    public static void main(String[] args) {
        Vector<MyPortListener> a = new Vector<MyPortListener>();
        MyPortListener b = new MyPortListener(1);
        a.add(new MyPortListener(1));
        a.add(new MyPortListener(1));
//        a.removeElement(b);
        a.remove(b);
        System.out.println(a.size());
    }

    public String getLastSuccessTime() {
        return lastSuccessTime;
    }
}
